
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintWriter;


/**
 * 这是适配不同屏幕的工具，需要运行java代码生成
 * 1. javac -encoding UTF-8 MakeXml.java
 * 2. java MakeXml
 * 3.会成成到C:/layouroot 目录下
 *
 * 如果不能执行javac 环境配置就有问题
 * JAVA_HOME：D:\Java\jdk1.8.0_91
 *
 * CLASSPATH：.;%JAVA_HOME%\lib\dt.jar;%JAVA_HOME%\lib\tools.jar;（前面有英文点号）
 *
 * Path：%JAVA_HOME%\bin;%JAVA_HOME%\jre\bin;
 * 这三个配置号就没问题
 */
public class MakeXml {
    // 生成地址 C盘layoutroot目录下，这个路径可以更改
    private final static String rootPath = "C:\\layoutroot\\values-{0}x{1}\\";


    /**
     * 设置基准分辨率
     * 一般标注按照多大的图标，这里我们就设置多大尺寸
     */
    private final static float dw = 1080f;
    private final static float dh = 1920f;


    private final static String WTemplate = "<dimen name=\"x{0}\">{1}px</dimen>\n";
    private final static String HTemplate = "<dimen name=\"y{0}\">{1}px</dimen>\n";


    // 手机分辨率
    public static void main(String [] args){
        makeString(320, 480);
        makeString(480, 800);
        makeString(480, 854);
        makeString(540, 960);
        makeString(600, 1024);
        makeString(720, 1184);
        makeString(720, 1196);
        makeString(720, 1280);
        makeString(768, 1024);
        makeString(800, 1280);
        makeString(1080, 1812);
        makeString(1080, 1920);
        makeString(1440, 2560);
        makeString(1600, 2560);
    }


    public static void makeString(int w, int h) {


        StringBuffer sb = new StringBuffer();
        sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
        sb.append("<resources>");
        float cellw = w / dw;
        for (int i = 1; i < dw; i++) {
            sb.append(WTemplate.replace("{0}", i + "").replace("{1}", change(cellw * i) + ""));
        }

        //此处可将1080换为自己的基准尺寸宽度
        sb.append(WTemplate.replace("{0}", "1080").replace("{1}", w + ""));
        sb.append("</resources>");


        StringBuffer sb2 = new StringBuffer();
        sb2.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
        sb2.append("<resources>");
        float cellh = h / dh;
        for (int i = 1; i < dh; i++) {
            sb2.append(HTemplate.replace("{0}", i + "").replace("{1}", change(cellh * i) + ""));
        }

        //此处可将1920换为自己的基准尺寸高度
        sb2.append(HTemplate.replace("{0}", "1920").replace("{1}", h + ""));
        sb2.append("</resources>");


        String path = rootPath.replace("{0}", h + "").replace("{1}", w + "");
        File rootFile = new File(path);
        if (!rootFile.exists()) {
            rootFile.mkdirs();
        }
        File layxFile = new File(path + "lay_x.xml");
        File layyFile = new File(path + "lay_y.xml");
        try {
            PrintWriter pw = new PrintWriter(new FileOutputStream(layxFile));
            pw.print(sb.toString());
            pw.close();
            pw = new PrintWriter(new FileOutputStream(layyFile));
            pw.print(sb2.toString());
            pw.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }


    }


    public static float change(float a) {
        int temp = (int) (a * 100);
        return temp / 100f;
    }


}
